﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using DoxygenBrowser.Common.Parser;

namespace GenTDictionaryFile
{
  public class TDictionaryAnalyser
  {
    private string _strTokenFilePath;
    private const string CONST_OUT_FILE = "tokens.tdic";
    private Dictionary<string, int> _stat;

    public TDictionaryAnalyser(string strPath)
    {
      _strTokenFilePath = strPath;
      _stat = new Dictionary<string, int>();
    }

    public bool ProcessTokensFile()
    {

      DocsetParser parser = new DocsetParser();
      StreamReader txtReader = new StreamReader(_strTokenFilePath, Encoding.ASCII);
      parser.TokenAddedEvent += new EventHandler<TokenAddedEventArgs>(this.HandleTokenAdded);

      parser.Parser(txtReader);

      return true;
    }

    public void HandleTokenAdded(object sender, TokenAddedEventArgs e)
    {
      if(!string.IsNullOrEmpty(e.TokenIdentifier.Type))        
      {
        if(!_stat.ContainsKey(e.TokenIdentifier.Type))
        {
          _stat.Add(e.TokenIdentifier.Type,0);
        }
        _stat[e.TokenIdentifier.Type]++;
        
      }
    }


    public void WriteResult(StringBuilder sb)
    {
      sb.AppendLine("Result:");
      foreach(KeyValuePair<string, int> obj in _stat)
      {
        sb.AppendFormat("{0}\t{1}", obj.Key, obj.Value);
        sb.AppendLine();
      }
      
    }
  }
}
